import React,{useEffect,useState} from "react";
import '../Cart/Cart.css'
import { useNavigate } from "react-router-dom";
import axios from "axios";
export default function Cart(){
    const [cart,setCart] = useState([])
    const Navigate = useNavigate()

    const getCart = async() => {
        let {data:{code,cart}}=await axios.get('/sUnJIahuI/cart')
        if(code === 200){
            setCart(cart)
        }
    }

    const totalPrice = cart.reduce((a,b)=>a+b.count*b.price,0)

    const totalNum = cart.reduce((a,b)=>a+b.count,0)

    const jian = async(_id,count) => {
        let obj = {_id:_id,count:count-1}
        let {data:{code}}=await axios.post('/sUnJIahuI/update',obj)
        if(code === 200){
            getCart()
        }
    }

    const jia = async(_id,count) => {
        let obj = {_id:_id,count:count+1}
        let {data:{code}}=await axios.post('/sUnJIahuI/update',obj)
        if(code === 200){
            getCart()
        }
    }

    const to = () => {
        Navigate('/show/cate')
    }

    const tototo = () => {
        Navigate('/CartConfrim')
    }
    

    useEffect(()=>{
        getCart()
    },[])
    
    return(
        <>
            <div className="cartBigBox">
                <div className="cartTopBox">
                    <span className="TopBoxText1">购物车</span>
                    <span className="TopBoxText2">编辑</span>
                </div>
                {
                    totalNum == 0 ?
                    <>
                        <div className="nullBox">
                            <svg t="1731071749102" className="icon111" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1535" width="128" height="128"><path d="M346.112 806.912q19.456 0 36.864 7.168t30.208 19.968 20.48 30.208 7.68 36.864-7.68 36.864-20.48 30.208-30.208 20.48-36.864 7.68q-20.48 0-37.888-7.68t-30.208-20.48-20.48-30.208-7.68-36.864 7.68-36.864 20.48-30.208 30.208-19.968 37.888-7.168zM772.096 808.96q19.456 0 37.376 7.168t30.72 19.968 20.48 30.208 7.68 36.864-7.68 36.864-20.48 30.208-30.72 20.48-37.376 7.68-36.864-7.68-30.208-20.48-20.48-30.208-7.68-36.864 7.68-36.864 20.48-30.208 30.208-19.968 36.864-7.168zM944.128 227.328q28.672 0 44.544 7.68t22.528 18.944 6.144 24.064-3.584 22.016-13.312 37.888-22.016 62.976-23.552 68.096-18.944 53.248q-13.312 40.96-33.28 56.832t-49.664 15.872l-35.84 0-65.536 0-86.016 0-96.256 0-253.952 0 14.336 92.16 517.12 0q49.152 0 49.152 41.984 0 20.48-9.728 35.328t-38.4 14.848l-49.152 0-94.208 0-118.784 0-119.808 0-99.328 0-55.296 0q-20.48 0-34.304-9.216t-23.04-24.064-14.848-32.256-8.704-32.768q-1.024-6.144-5.632-29.696t-11.264-58.88-14.848-78.848-16.384-87.552q-19.456-103.424-44.032-230.4l-76.8 0q-15.36 0-25.6-7.68t-16.896-18.432-9.216-23.04-2.56-22.528q0-20.48 13.824-33.792t37.376-13.312l21.504 0 21.504 0 25.6 0 34.816 0q20.48 0 32.768 6.144t19.456 15.36 10.24 19.456 5.12 17.408q2.048 8.192 4.096 23.04t4.096 30.208q3.072 18.432 6.144 38.912l700.416 0zM867.328 194.56l-374.784 0 135.168-135.168q23.552-23.552 51.712-24.064t51.712 23.04z" p-id="1536" fill="#9cb87c"></path></svg>
                            <p className="nullBoxText">购物车什么都没有，赶快去加购吧！</p>
                            <button className="nullBoxButton" onClick={()=>to()}>去逛逛</button>
                        </div>
                    </>:
                    <div className="cartContentBox">
                        {
                          cart.filter(item=>item.count > 0).map((item)=>{
                              return <div key={item._id} className="cartBox">
                                  {/* <input type="checkbox" className="cartCheck"></input> */}
                                  {/* <div className="cartSmallBox"></div> */}
                                  <img src={item.img} className="cartImg"></img>
                                  <p className="cartText1">{item.name}</p>
                                  <p className="cartText2">{item.specs}枝/扎</p>
                                  <p className="cartText3">￥{item.price}</p>
                                  <div className="cartButtonBox">
                                      <button onClick={()=>jian(item._id,item.count)}>-</button>
                                      <button>{item.count}</button>
                                      <button onClick={()=>jia(item._id,item.count)}>+</button>
                                  </div>
                              </div>
                          })
                        }
                  </div>
                }
              
                <div className="cartButtomBox">
                    <span className="buttomBoxText1">合计：</span>
                    <span className="buttomBoxText2">￥{totalPrice}</span>
                    <button className="buttomBoxButton" onClick={()=>tototo()}>结算（{totalNum}）</button>
                </div>
            </div>
        </>
    )
}